package com.ruoyi.project.kkad.channelswitch.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kkad.channelswitch.domain.ChannelSwitch;
import com.ruoyi.project.kkad.channelswitch.service.IChannelSwitchService;

/**
 * 渠道配置Controller
 * 
 * @author shixiangwen
 * @date 2020-04-13
 */
@Controller
@RequestMapping("/kkad/channelswitch")
public class ChannelSwitchController extends BaseController {

	private String prefix = "kkad/channelswitch";

	@Autowired
	private IChannelSwitchService channelSwitchService;

	@RequiresPermissions("kkad:channelswitch:view")
	@GetMapping()
	public String channelswitch() {
		return prefix + "/channelswitch";
	}

	/**
	 * 查询渠道配置列表
	 */
	@RequiresPermissions("kkad:channelswitch:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ChannelSwitch channelSwitch) {
		startPage();
		List<ChannelSwitch> list = channelSwitchService.selectChannelSwitchList(channelSwitch);
		return getDataTable(list);
	}

	/**
	 * 导出渠道配置列表
	 */
	@RequiresPermissions("kkad:channelswitch:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(ChannelSwitch channelSwitch) {
		List<ChannelSwitch> list = channelSwitchService.selectChannelSwitchList(channelSwitch);
		ExcelUtil<ChannelSwitch> util = new ExcelUtil<ChannelSwitch>(ChannelSwitch.class);
		return util.exportExcel(list, "channelswitch");
	}

	/**
	 * 新增渠道配置
	 */
	@GetMapping("/add/{appId}")
	public String add(@PathVariable("appId") String appId, ModelMap mmap) {
		mmap.addAttribute("appId", appId);
		return prefix + "/add";
	}

	/**
	 * 新增渠道配置
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存渠道配置
	 */
	@RequiresPermissions("kkad:channelswitch:add")
	@Log(title = "渠道配置", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ChannelSwitch channelSwitch) {
		channelSwitch.setCreatedUserId(ShiroUtils.getLoginName());
		channelSwitch.setCreatedTime(DateUtils.getNowDate());
		return toAjax(channelSwitchService.insertChannelSwitch(channelSwitch));
	}

	/**
	 * 修改渠道配置
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		ChannelSwitch channelSwitch = channelSwitchService.selectChannelSwitchById(id);
		mmap.put("channelSwitch", channelSwitch);
		return prefix + "/edit";
	}

	/**
	 * 修改保存渠道配置
	 */
	@RequiresPermissions("kkad:channelswitch:edit")
	@Log(title = "渠道配置", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ChannelSwitch channelSwitch) {
		return toAjax(channelSwitchService.updateChannelSwitch(channelSwitch));
	}

	/**
	 * 删除渠道配置
	 */
	@RequiresPermissions("kkad:channelswitch:remove")
	@Log(title = "渠道配置", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(channelSwitchService.deleteChannelSwitchByIds(ids));
	}
}
